﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Web.Services;
using System.Data.SqlClient;
using DB;

/// <summary>
///PaymentService 的摘要说明
/// </summary>
[WebService(Namespace = "http://lab365.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。 
// [System.Web.Script.Services.ScriptService]
public class PaymentService : System.Web.Services.WebService {

    public PaymentService () {

        //如果使用设计的组件，请取消注释以下行 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string[] Transfer(string from, string to, string password, float amount)
    {
        string[] res = new string[2];
        bool isSucc = false;
        string errorMsg = "";
        float balace = 0;
        DataAccess da = new DataAccess();
        SqlDataReader dr = da.GetDataReader("select balance from accounts where cardNo='" + from + "' and userPassword='" + password + "'");
        if (dr.Read())
        {
            if (!IsExists(to))       //'to' dose not exist
            {
                isSucc = false;
                errorMsg = "转入账户不存在!请核实账户信息";
            }
            else
            {
                balace = float.Parse(dr.GetValue(0).ToString());
                if (balace < amount)     //balance is not enough
                {
                    isSucc = false;
                    errorMsg = "余额不足!";
                }
                else
                {
                    if (Transfer(from, to, amount))
                    {
                        isSucc = true;
                    }
                    else                        //some error
                    {
                        isSucc = false;
                        errorMsg = "操作失败!请重试.";
                    }
                }
            }
        }
        else                                   //'from' dose not exist or password is not correct
        {
            isSucc = false;
            errorMsg = "卡号不存在或者密码不正确!";
        }
        res[0] = isSucc == true ? "T" : "F";
        res[1] = errorMsg;
        return res;
    }

    private bool IsExists(string cardNo)
    {
        DataAccess da = new DataAccess();
        SqlDataReader dr = da.GetDataReader("select cardNo from accounts where cardNo='" + cardNo + "'");
        if (dr.Read())
            return true;
        return false;
    }

    private bool Transfer(string from, string to, float amount)
    {
        StringBuilder strb=new StringBuilder();
        DataAccess da = new DataAccess();
        strb.Append("insert into accountlog(srcCardNo,desCardNo,amount,time) values('" + from + "','" + to + "','" + amount.ToString() + "',getdate())");
        da.RunSql(strb.ToString());
        strb = new StringBuilder();
        strb.Append("update accounts set balance=balance-" + amount.ToString() + " where cardNo='" + from + "'");
        strb.Append(";");
        strb.Append("update accounts set balance=balance+" + amount.ToString() + " where cardNo='" + to + "'");
        da.RunSql(strb.ToString());
        return true;
    }
}

